package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.PaasPrintTemplateDao;
import com.xbongbong.paas.domain.entity.PaasPrintTemplateEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.print.pojo.pojo.dto.PaasPrintListDTO;
import com.xbongbong.pro.print.pojo.pojo.vo.PaasPrintItemVO;
import com.xbongbong.pro.print.pojo.pojo.vo.PaasPrintListVO;
import com.xbongbong.saas.domain.entity.ext.WorkOrderFormEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.WorkOrderFormModel;
import com.xbongbong.saas.service.PrintMobileService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author guohui.zhang
 * @Title: 移动端支持打印
 * @date 2020/10/1914:08
 */
@Service("printMobileServiceImpl")
public class PrintMobileServiceImpl implements PrintMobileService {

    @Resource
    private PaasPrintTemplateDao paasPrintTemplateDao;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private WorkOrderFormModel workOrderFormModel;

    @Override
    public PaasPrintListVO list(PaasPrintListDTO paasPrintListDTO) throws XbbException {
        PaasPrintListVO  paasPrintListVO = new PaasPrintListVO();
        List<PaasPrintTemplateEntity> list;
        String corpid = paasPrintListDTO.getCorpid();
        // 查询关联表单
        List<Long> formIdIn = new ArrayList<>();
        // businessType
        Integer businessType =null;
        // 表单id-名称映射
        Map<Long,String> formNameMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        HashMap<String,Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        try {
            params.put("corpid", paasPrintListDTO.getCorpid());
            params.put("appId", paasPrintListDTO.getAppId());
            params.put("menuId", paasPrintListDTO.getMenuId());
            params.put("del", DelEnum.NORMAL.getDel());
            String alias = paasPrintListDTO.getAlias();
            if (Objects.equals(alias, XbbRefTypeEnum.WORK_ORDER.getAlias()) || Objects.equals(paasPrintListDTO.getBusinessType(),XbbRefTypeEnum.WORK_ORDER.getCode())){
                // 根据工单模板id查询对应的工单表单id,继而查询工单默认打印模板
                WorkOrderFormEntityExt byTemplateId = null;
                if (!Objects.equals(paasPrintListDTO.getTemplateId(),0L)) {
                    // 因为不同接口返回的值不同,所以进行特殊处理
                    if (Objects.nonNull(paasPrintListDTO.getTemplateId())) {
                        byTemplateId = workOrderFormModel.getByTemplateId(paasPrintListDTO.getTemplateId(), paasPrintListDTO.getCorpid());
                    }else {
                        byTemplateId = workOrderFormModel.getByKey(paasPrintListDTO.getFormId(), paasPrintListDTO.getCorpid());
                    }
                    businessType = XbbRefTypeEnum.WORK_ORDER.getCode();
                    formNameMap.put(byTemplateId.getId(),byTemplateId.getName());
                }else{
                    return paasPrintListVO;
                }
                params.remove("menuId");
                params.put("formId",byTemplateId.getId());
                list = paasPrintTemplateDao.findEntitys(params);
            }else{
                params.put("formId", paasPrintListDTO.getFormId());
                params.put("columns", "id,app_id,form_id,name,config_info");
                list = paasPrintTemplateDao.findEntitys(params);
            }

        } catch (Exception e) {

            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        if (CollectionsUtil.isEmpty(list)) {
            list = new ArrayList<>();
        }
        List<PaasPrintItemVO> listPrintList = new ArrayList<>();
        for (PaasPrintTemplateEntity paasPrintTemplateEntity : list) {
            Long formId = paasPrintTemplateEntity.getFormId();
            formIdIn.add(formId);
        }
        List<PaasFormEntityExt> paasFormEntityList = null;
        if (!Objects.equals(XbbRefTypeEnum.WORK_ORDER.getAlias(),paasPrintListDTO.getAlias()) && !Objects.equals(XbbRefTypeEnum.WORK_ORDER.getCode(),paasPrintListDTO.getBusinessType())) {
            params.clear();
            params.put("corpid",corpid);
            params.put("appId",paasPrintListDTO.getAppId());
            params.put("menuId",paasPrintListDTO.getMenuId());
            params.put("idIn",formIdIn);
            paasFormEntityList = paasFormModel.list(params);
            if (CollectionsUtil.isEmpty(paasFormEntityList)){
                return  paasPrintListVO;
            }
            businessType=paasFormEntityList.get(0).getBusinessType();
            paasFormEntityList.forEach((paasFormEntity)->{
                String name = paasFormEntity.getName();
                Long id = paasFormEntity.getId();
                formNameMap.put(id,name);
            });
        }
        for (PaasPrintTemplateEntity paasPrintTemplateEntity : list) {
            Long formId = paasPrintTemplateEntity.getFormId();
            String formName = formNameMap.get(formId) == null ? "" : formNameMap.get(formId);
            // 判断模板是否三联单
            JSONObject configInfo = JsonHelperUtil.parseObject(paasPrintTemplateEntity.getConfigInfo());
            String size = configInfo.getString("size") == null ? "" : configInfo.getString("size");
            int isDivision = size.contains("division") ? 1 : 0;
            PaasPrintItemVO paasPrintItemVO = new PaasPrintItemVO();
            paasPrintItemVO.setAppId(paasPrintTemplateEntity.getAppId());
            paasPrintItemVO.setMenuId(paasPrintTemplateEntity.getMenuId());
            paasPrintItemVO.setFormId(formId);
            paasPrintItemVO.setFormName(formName);
            paasPrintItemVO.setId(paasPrintTemplateEntity.getId());
            paasPrintItemVO.setName(paasPrintTemplateEntity.getName());
            paasPrintItemVO.setBusinessType(businessType);
            paasPrintItemVO.setIsDivision(isDivision);
            listPrintList.add(paasPrintItemVO);
        }
        paasPrintListVO.setListPrintList(listPrintList);
        return paasPrintListVO;
    }
}
